﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using IdleTest.MSTest;
using IdleTest.EFAndMVCDemo.Models;
using IdleTest.EFAndMVCDemo.MvcUI.Controllers;
using IdleTest.EFAndMVCDemo.IServices.Fakes;

namespace IdleTest.EFAndMVCDemo.MvcUITest
{
    [TestClass]
    public class UserControllerTest
    {
        private UserController controller;

        private string beforeURL = "/User/About";


        [TestInitialize]
        public void InitTest()
        {
            StubIUserService userService = new StubIUserService();
            //模拟用户输入了正确的用户名和密码
            userService.LoginUserModel = p => 
                p.LoginName == UITestConfig.ExistsUserName && p.Password == UITestConfig.ExistsPassword;
            controller = new UserController(userService);
        }

        #region Login
        [TestMethod]
        public void LoginTest_进入登陆页面不出异常()
        {
            //确保Action在参数为空时不会出异常
            AssertCommon.ThrowException<string>(TestCommon.GetEmptyStrings(), false, p => controller.Login(p));
        }

        [TestMethod]
        public void LoginTest_进入正确的登陆页面地址()
        {
            LoginGetTestHelper(controller.Login(beforeURL));
            LoginGetTestHelper(controller.Login(null));
        }

        private void LoginGetTestHelper(ActionResult view)
        {
            ControllerAssert.AssertViewResult(view, UITestConfig.LoginViewName);
        }

        [TestMethod]
        public void LoginTest_登陆提交不出异常()
        {
            //确保Action在参数为空时不会出异常
            AssertCommon.ThrowException<string>(TestCommon.GetEmptyStrings(), false, p => controller.Login(p, null, null));
            AssertCommon.ThrowException<string>(TestCommon.GetEmptyStrings(), false, p => controller.Login(null, p, null));
            AssertCommon.ThrowException<string>(TestCommon.GetEmptyStrings(), false, p => controller.Login(null, null, p));
        }

        [TestMethod]
        public void LoginPostTest_登陆提交用户名或密码错误_回到登陆页面()
        {
            //用户名或密码错误均不能登录，返回的view均为“Login”
            LoginGetTestHelper(controller.Login(UITestConfig.NotExistsUserName, UITestConfig.NotExistsPassword, null));
            LoginGetTestHelper(controller.Login(UITestConfig.ExistsUserName, UITestConfig.NotExistsPassword, null));
            LoginGetTestHelper(controller.Login(UITestConfig.NotExistsUserName, UITestConfig.ExistsPassword, null));
        }

        [TestMethod]
        public void LoginPostTest_登陆提交用户名或密码为空_回到登陆页面()
        {
            //用户名或密码为空均不能登录，返回的view均为“Login”
            LoginGetTestHelper(controller.Login(UITestConfig.ExistsUserName, null, null));
            LoginGetTestHelper(controller.Login(null, UITestConfig.ExistsPassword, null));
        }

        [TestMethod]
        public void LoginPostTest_登陆提交用户名或密码正确_进入指定页面()
        {
            LoginSuccessTest(controller.Login(
                UITestConfig.ExistsUserName, UITestConfig.ExistsPassword, beforeURL), beforeURL);

            LoginSuccessTest(controller.Login(
                UITestConfig.ExistsUserName, UITestConfig.ExistsPassword, null));
        }

        private void LoginSuccessTest(ActionResult view, string expectedUrl = null)
        {
            if (string.IsNullOrEmpty(expectedUrl))
                expectedUrl = UITestConfig.DefaultUserUrl;

            ControllerAssert.AssertRedirectResult(view, expectedUrl);
        }
        #endregion

        [TestMethod]
        public void RegisterGetTest()
        {
        }

        [TestMethod]
        public void RegisterPostTest()
        {
        }
    }
}
